<template>
	<om-addpage style="padding-top: 35px;" :menuName="menuName" @formSubmit="formSubmit" :busCode="busCode">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		getUrl,
		confirmModal,
		errorModal,
		ptFloat,
		minusF,
		ignoreHtml
	} from '@/static/js/common.js';
	import {
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	import moment from '@/static/js/moment.js';
	export default {
		data() {
			return buildFormData({
				posIdCahce: "hr-u-delay-chahce",
				userIdCacheKey: "hr-u-delay-userId-cache"
			})
		},
		provide() {
			return {
				formPage: this
			}
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods: {
			onSelectDate(date){
				let _that = this
				if(!_that.$refs.autoform.formdata.userId){
					let title  = _that.$refs.autoform.getEditorTitle("userInfo","userId")
					_that.om.toast('请先选择'+title)
					_that.$refs.autoform.clear("posInfo","adjustProbationDate")
				}else{
					if(moment(date).isSameOrBefore(_that.$refs.autoform.formdata.origProbationDate)){
						errorModal(_that,'提示信息','无法选择'+_that.$refs.autoform.formdata.origProbationDate+'和之前的日期',()=>{
							_that.$refs.autoform.clear("posInfo","adjustProbationDate")
						})
					}
				}
			},
			formSubmit() {
				let _that = this
				_that.om.formSave('wxProbationDelay/save', _that.formdata, _that);
			},

			onZposFunc(item) {
				let _that = this
				let obj = {
					posId: item.id
				}
				_that.om.request(
					'probationUserList/getUserListByParentPosId',
					obj,
					'get'
				).then(res => {
					//if (res instanceof Array) {
					if(Object.prototype.toString.call(res)==='[object Array]'){
						if (!res.length) {
							_that.om.toast('当前制表岗位没有处于试用期的下级人员', 3000)
							return
						}
						_that.$refs.autoform.changeEditor('userInfo', 'userId', 'data', configSelectData(res, 'id', 'username'))
					}
				})
			},
			initUserId(id) {
				let _that = this
				let obj = {
					posId: id
				}
				_that.om.request(
					'probationUserList/getUserListByParentPosId',
					obj,
					'get'
				).then(res => {
					//if (res instanceof Array) {
					if(Object.prototype.toString.call(res)==='[object Array]'){
						if (!res.length) {
							_that.om.toast('当前制表岗位没有处于试用期的下级人员', 3000)
							return
						}
						_that.$refs.autoform.changeEditor('userInfo', 'userId', 'data', configSelectData(res, 'id', 'username'))
						_that.$refs.autoform.initSelectIndex("userInfo","userId")
					}
				})
			},
			/**
			 * 选择姓名验证
			 */
			onSelectUser(item) {
				let _that = this
				_that.om.request('wxProbationDelay/verifyUser', {
					userId: item.id
				}).then(res => {
					if (res.succeed) {
						if(res.value){
							errorModal(_that,'提示信息',ignoreHtml(res.value),()=>{
								_that.$refs.autoform.clear("userInfo","userId")
							})
						}else{
							_that.origProbationDateFunc(item.id);
						}
					} else {
						errorModal(_that,'提示信息',ignoreHtml(res.msg),()=>{
							_that.$refs.autoform.clear("userInfo","userId")
						})
					}
				})
			},
			/**
			 * 原试用期到期日
			 */
			origProbationDateFunc(id) {
				let _that = this;
				_that.om.request('wxProbationDelay/getNewDataInfo', {
					userId: id
				}).then(res => {
					if (res.succeed) {
						_that.$refs.autoform.formdata.origProbationDate = res.value.posResponse.origProbationDate
						_that.$refs.autoform.formdata.sexName = res.value.userResponse.sexName
						_that.$refs.autoform.formdata.birthPlace = res.value.userResponse.birthPlace
						_that.$refs.autoform.formdata.birthDate = res.value.userResponse.birthDate
						_that.$refs.autoform.formdata.heigestEdu = res.value.userResponse.heigestEdu
						_that.$refs.autoform.formdata.sexName = res.value.userResponse.sexName
						_that.$refs.autoform.formdata.entryDate = res.value.userResponse.entryDate
						_that.$refs.autoform.formdata.workDate = res.value.userResponse.workDate
						_that.$refs.autoform.formdata.posName = res.value.posResponse.posName
						_that.$refs.autoform.formdata.instId = res.value.posResponse.instId
						_that.$refs.autoform.formdata.instName = res.value.posResponse.instName
						_that.$refs.autoform.formdata.salaryWay = res.value.posResponse.salaryWay
						_that.$refs.autoform.formdata.posGrade = res.value.posResponse.posGrade
						_that.$refs.autoform.formdata.gradeSalaryRange = res.value.posResponse.gradeSalaryRange
						_that.$refs.autoform.formdata.posFeature = res.value.posResponse.posFeature
						_that.$refs.autoform.formdata.probationType = res.value.posResponse.probationType
						_that.$refs.autoform.formdata.probation = res.value.posResponse.probation
						_that.$refs.autoform.formdata.probationMonth = res.value.scoreResponse.probationMonth
						_that.$refs.autoform.formdata.num1 = res.value.scoreResponse.num1
						_that.$refs.autoform.formdata.num2 = res.value.scoreResponse.num2
						_that.$refs.autoform.formdata.num3 = res.value.scoreResponse.num3
						_that.$refs.autoform.formdata.num4 = res.value.scoreResponse.num4
						_that.om.request(
							"tinstitution/getInstInfo",
							{
								instId:res.value.posResponse.instId
							}
						).then(res=>{
							_that.$refs.autoform.formdata.instLevelId =  res.value.instcatId
							_that.$refs.autoform.formdata.instLevelName =  res.value.instcatName
						})
					} else {
						_that.om.toast(ignoreHtml(res.msg))
					}
				})
			},

			initLoad(options) {
				let _that = this
				let ajaxArr = []
				var fileId = options.fileId
				ajaxArr.push(urlUtil.zposUrl(options.code))
				if(fileId){
					ajaxArr.push({
						url:"tprobationDelay/getInfo",
						param:{
							id:fileId
						},
						method:"post"
					})
				}
				formModelLoad(ajaxArr, function(res, mainData) {
					if (res[0].length) {
						changeEditor('topinfo', 'zposId', 'data', configSelectData(res[0], 'id', 'posName'), mainData)
					} else {
						_that.om.toast('您在OM系统中没有该表单权限,无法查看', 3000)
					}
					if(fileId){
						var fieldArr = getFormFields(mainData)
						fieldArr.push("id")
							for(var j in res[1].value.tprobationDelay){
								if(fieldArr.includes(j)){
									_that.$refs.autoform.$set(_that.$refs.autoform.formdata,j,res[1].value.tprobationDelay[j])
							}
						}
					}
					_that.$nextTick(function(){
						if(!_that.$refs.autoform.formdata.zposId){
							_that.$refs.autoform.initSelectVal("topinfo","zposId")
						}else{
							_that.$refs.autoform.initSelectIndex("topinfo","zposId")
						}
						_that.initUserId(_that.$refs.autoform.formdata.zposId)
					})
				}, options)
			}
		},
		onShow() {

		},
		onLoad(options) {
			this.initLoad(options)
		}
	}
</script>

<style>
</style>
